package com.dalin.framework.dao.imple;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.dalin.framework.dao.DbConn;
import com.dalin.framework.vos.ColumnBaseInfoVo;
import com.dalin.platform.loa.LoaConst;

/*****************************************************************************
 * @content: oracle连接数据库实现                                             *
 * @author : 那建林                                                          *
 * @crtDate: 2009-06-15                                                     *
 * @version: 1.01                                                           *
 ****************************************************************************/
public class OracleConnection extends AbstractConnection implements DbConn {
	public OracleConnection(String driverClassName, String url,
			String userName, String userPswd) {
		super(driverClassName, url, userName, userPswd);
	}
	
	public List<Map> queryData(String sql) throws Exception {
		return super.queryData(sql);
	}

	public int executeUpdate(String sql) throws Exception {
		return super.executeUpdate(sql);
	}

	public Set<String> queryTables() throws Exception {
		Set<String> rtnSet= new HashSet<String>();
		List<Map> dataList = super.queryData(
				"select table_name from user_tables t where t.table_name not like 'BIN%'");
		for (Map m : dataList) {
			rtnSet.add(m.get("TABLE_NAME").toString());
		}
		return rtnSet;
	}

	public Set<ColumnBaseInfoVo> queryColumnInfosByTable(
			String tableName) throws Exception {
		return super.queryColumnInfosByTableName(tableName);
	}

	public ColumnBaseInfoVo findColumnInfoByColumnName(
			String tableName, String columnName) throws Exception {
		return super.findColumnInfoByColumnName(tableName, columnName);
	}
	
	
	public String getJavaTypeFromColumnType(
			String columnType, int columnLength, int decimalLength) throws Exception {
		if ("NUMBER".equals(columnType)) {
			if (decimalLength > 0) {
				return "Double";
			} else if (columnLength < 9) {
				return "Integer";
			} else {
				return "Long";
			}
		} else if ("DATE".equals(columnType)) {
			return "Date";
		} else {
			return "String";
		}
	}

	public boolean testConnDatabase() throws Exception {
		return super.openConnection() == null ? false : true;
	}

	public void addColumn(String tableName, String columnName,
			String columnType, Integer columnLength, boolean nullable)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("ALTER TABLE ").append(tableName)
			.append(" ADD ").append(columnName)
			.append(" ").append(columnType);
		if (!"DATE".equalsIgnoreCase(columnType)) {
			sb.append("(").append(columnLength).append(")");
		}
		if (!nullable) {
			sb.append(" not null");
		}
		this.executeUpdate(sb.toString());
	}
	
	//-520需要改进成批量执行的
	public void alterColumn(String tableName, 
					String originalColumnName,
					String newColumnName,
					String columnType,
					Integer columnLength,
					boolean nullable) throws Exception {
		if (!newColumnName.equals(originalColumnName)) {
			StringBuffer sb = new StringBuffer();
			sb.append("ALTER TABLE ").append(tableName)
				.append(" RENAME COLUMN ").append(originalColumnName)
				.append(" TO ").append(newColumnName);
			this.executeUpdate(sb.toString());
		}
		StringBuffer sb = new StringBuffer();
		sb.append("ALTER TABLE ").append(tableName)
			.append(" MODIFY ").append(newColumnName)
			.append(" ").append(columnType);
		if (!"DATE".equalsIgnoreCase(columnType)) {
			sb.append("(").append(columnLength).append(")");
		}
		
		ColumnBaseInfoVo cbf = findColumnInfoByColumnName(tableName, newColumnName);
		if (!nullable && cbf.getNullable() != 0) {
			sb.append(" not null");
		}
		this.executeUpdate(sb.toString());
	}
	
	public void dropColumn(String tableName,String columnName) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("ALTER TABLE ").append(tableName)
			.append(" DROP COLUMN ").append(columnName);
		this.executeUpdate(sb.toString());
	}

	public Serializable createObj(Object entity) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
